#include "addtime.h"
#include "ui_addtime.h"
#include <QMessageBox>
AddTime::AddTime(QSettings *settings, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::AddTime)
{
    mSettings=settings;
    ui->setupUi(this);
    connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(onBtnClicked()));
}

AddTime::~AddTime()
{
    delete ui;
}

QTime AddTime::start() const
{
    return mStart;
}

QTime AddTime::end() const
{
    return mEnd;
}

bool AddTime::canAddTime(QTime start, QTime end)
{
    QStringList list=mSettings->value("time").toStringList();
    for(QString s:list){
        QTime st,en;
        QStringList tmp=s.split("->");
        st=QTime::fromString(tmp[0],"HH:mm");
        en=QTime::fromString(tmp[1],"HH:mm");
        if(!(start>=en||end<=st))
        {
            return false;
        }
    }
    return true;
}


void AddTime::onBtnClicked()
{
    mStart=ui->timeEdit->time();
    mEnd=ui->timeEdit_2->time();
    if(mStart>=mEnd)
    {
        QMessageBox::critical(this,"错误","起始时间应小于结束时间!");
    }
    else if(!canAddTime(mStart,mEnd))
    {

        QMessageBox::critical(this,"错误","该时间段与其他时间段冲突!");
    }
    else
    {
        accept();
    }
}
